/**
 * Created by wpj on 2019-3-10
 */

/**
 * 手机号验证
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 */
var checkPhone = function (rule, value, callback) {
    if (!value) {
        return callback(new Error('手机号不能为空'));
    }
    if (!$w.isMobile(value)) {
        return callback(new Error('请输入正确的手机号'));
    }
    return callback();
};

/**
 * 验证规则等于 true
 * @param rule
 * @param value
 * @param callback
 * @returns {*}
 */
var checkAgree = function (rule, value, callback) {
    if (!value) {
        return callback(new Error('要注册必须同意条目'));
    } else {
        callback();
    }
};

/**
 *
 * 注册
 * @param baseParams
 * @returns {*}
 */
var register = function (baseParams) {

    return new Vue({
        el: '#app',
        data: {
            detailOver: true, // 详情加载完毕
            registerData: {
                phone: '', // 手机号
                passWord: '', // 密码
                mobileCode: '', // 手机验证码
                code: '', // 验证码
                agree: true, //是否同意平台规定
                spread_uid: '' // 上级用户编号
            },
            regDiaVisible: false, // 注册同意条目
            agreeData: {
                title: '《注册协议》',
                detail: ''
            }, // 平台规范一些数据
            captcha: '',
            passInputType: '', // 密码是否显示明文
            submitLoading: false, // 登录按钮 正在加载中
            isSendMCode: false, // 是否点击获取了验证码
            mCodeSecond: 60, // 手机验证码倒计时
            inviterId: null // 传输过来的邀请人编号
        },
        created: function () {
            // 获取下验证码
            this.reCaptcha();
            this.initDetail();
        },
        methods: {
            initDetail: function () {

                this.agreeData.detail = '同意条款';
                return false;
                // 正在加载。。
                var loadingInstance = ELEMENT.Loading.service({
                    fullscreen: false,
                    text: '加载中...',
                    customClass: 'z-index-top'
                });

                var that = this;
                var key = baseParams.settingKey['regAgree'];

                // 获取各模块的值
                $w.request({
                    url: baseParams.url.userGetSetting,
                    type: 'get',
                    data: {
                        key: key
                    },
                    dataType: "json",
                    afterCallback: function () {
                        that.$nextTick(function () {
                            that.loadOver = true;
                            // 隐藏正在加载
                            loadingInstance.close();
                        });
                    },
                    callback: function (event) {

                        // 失败的返回|提示
                        if (parseInt(event.no) !== 200) {

                            return that.$message({
                                showClose: true,
                                type: 'error',
                                message: event.msg ? event.msg : '操作失败，请稍后尝试'
                            });
                        }

                        that.agreeData.detail = event.data.value;
                    }
                });
            },
            /**
             * 注册
             */
            register: function () {

                // 按钮正在加载
                this.submitLoading = true;
                var that = this;

                // 先验证下
                this.$refs['registerForm'].validate(function(valid) {
                    // 验证不过
                    if (!valid) {
                        that.submitLoading = false;
                        return false;
                    }

                    // 验证成功 - 继续发送请求
                    // 获取各模块的值
                    $w.request({
                        url: baseParams.url.register,
                        type: 'POST',
                        data: {
                            phone: that.registerData.phone,
                            username: that.registerData.userName,
                            password: that.registerData.passWord,
                            verifyCode: that.registerData.code,
                            mobileCode: that.registerData.mobileCode,
                            spread_uid: that.registerData.spread_uid
                        },
                        dataType: "json",
                        afterCallback: function () {
                            that.$nextTick(function () {
                                // 按钮正在加载
                                that.submitLoading = false;
                            });
                        },
                        callback: function (event) {

                            // 失败的返回|提示
                            if (parseInt(event.no) !== 200) {

                                return that.$message({
                                    showClose: true,
                                    type: 'error',
                                    message: event.msg ? event.msg : '操作失败，请稍后尝试'
                                });
                            }

                            window.parent.location.href = $w.getPageUrl('login');
                        }
                    });
                });
            },
            /**
             * 重新加载验证码
             */
            reCaptcha: function () {

                var that = this;
                // 刷新
                var str = '?c=' +
                    $w.getRandStr() +
                    '.' +
                    Math.ceil(Math.random() * 10000000000);
                that.captcha = $w.getApiUrl('reCaptcha') + str;
                // 清空已经填写验证码
                that.registerData.code = '';
            },
            /**
             * 隐藏|显示 密码框密码
             */
            showHidePass: function () {
                if (!this.passInputType || this.passInputType === 'text') {
                    this.passInputType = 'password';
                    return true;
                } else {

                    this.passInputType = 'text';
                    return true;
                }
            },
            /**
             * 获取手机验证码
             */
            getMobileCode: function () {

                var that = this;

                // 验证手机号合法
                var mobile = this.registerData.phone;
                const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
                if (!reg.test(mobile)) {

                    return that.$message({
                        type: 'error',
                        showClose: true,
                        message: '请输入合法的手机号码'
                    });
                }

                this.isSendMCode = true; // 已发送 状态
                // 获取各模块的值
                $w.request({
                    url: baseParams.url.getRegCode,
                    type: 'POST',
                    data: {
                        phone: this.registerData.phone
                    },
                    dataType: "json",
                    callback: function (event) {

                        // 失败的返回|提示
                        if (parseInt(event.no) !== 200) {

                            return that.$message({
                                showClose: true,
                                type: 'error',
                                message: event.msg ? event.msg : '操作失败，请稍后尝试'
                            });
                        }

                        that.mCodeSecond = 60; // 默认秒
                        var timer = setInterval(function () {
                            // 如果时间是0啦，可以点击了 && 清除计时器
                            if (that.mCodeSecond <= 0) {

                                clearInterval(timer);
                            } else { // 否则继续减减

                                that.mCodeSecond--;
                            }
                        }, 1000);
                    }
                });
            }
        },
        computed: {
            rules: function () {
                if (!this.detailOver) return {};
                return {
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {validator: checkPhone, trigger: 'blur'}
                    ],
                    passWord: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    mobileCode: [
                        {required: true, message: '请输入手机验证码', trigger: 'blur'}
                    ],
                    code: [
                        {required: true, message: '请输入验证码', trigger: 'blur'}
                    ],
                    agree: [
                        {validator: checkAgree}
                    ]
                }
            }
        }
    });
};
